package com.fingard.dsp.bank.directbank.ccb02;

import com.fingard.dsp.bank.directbank.bankInterface.IBalanceCheckConfirm;
import com.fingard.dsp.bank.reqrespobj.balancecheckconfirm.ReqBalCheckConfirmQry;
import com.fingard.dsp.bank.reqrespobj.balancecheckconfirm.RetAtsBalCheckConfirmQry;
import com.fingard.dsp.bank.reqrespobj.balancecheckconfirm.detail.ReqBalCheckConfirmDetail;
import com.fingard.dsp.bank.reqrespobj.balancecheckconfirm.detail.RetAtsBalCheckConfirmDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.util.ArrayList;
import java.util.List;


public class CCB02BalanceCheckConfirm extends CCB02Base implements IBalanceCheckConfirm {
    @Override
    public RetAtsBalCheckConfirmQry d_7509_BalCheckConfirm(ReqBalCheckConfirmQry p_req7509) throws Exception {
        RetAtsBalCheckConfirmQry retAts = new RetAtsBalCheckConfirmQry(p_req7509.reqHeader);
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            CCB02Header bankHeader = new CCB02Header();
            bankHeader.TX_CODE = "6WA006";
            bankHeader.REQUEST_SN = p_req7509.reqHeader.transSeqID;
            writeCommonHeader(xmlWriter, bankHeader);
            xmlWriter.writeStartElement("TX_INFO");
            List<ReqBalCheckConfirmDetail> actNumList = new ArrayList<ReqBalCheckConfirmDetail>();
            while (p_req7509.readRecord()) {
                xmlWriter.writeStartElement("LIST");
                xmlWriter.writeElementString("ACCT_NO", p_req7509.readedDetail.actNum);//账号
                actNumList.add(p_req7509.readedDetail);
                xmlWriter.writeElementString("BILL_NO", p_req7509.readedDetail.statementNumber);//对账单编号
                xmlWriter.writeEndElement();//LIST
            }
            xmlWriter.writeEndElement();//TX_INFO
            xmlWriter.writeElementClosed("SIGN_INFO");
            xmlWriter.writeEndDocument();

            String[] tmpRetBkStr = sendToBank(xmlWriter.toXmlString());
//            String[] tmpRetBkStr = {"", "<?xml version=\"1.0\" encoding=\"GB2312\" standalone=\"yes\" ?>  \n" +
//                    "<TX>  \n" +
//                    "  <REQUEST_SN>test000001</REQUEST_SN>  \n" +
//                    "  <CUST_ID>test000002</CUST_ID>  \n" +
//                    "  <TX_CODE>6WA006</TX_CODE>  \n" +
//                    "  <RETURN_CODE>000000</RETURN_CODE>  \n" +
//                    "  <RETURN_MSG>处理成功</RETURN_MSG>  \n" +
//                    "  <LANGUAGE>CN</LANGUAGE>  \n" +
//                    "  <TX_INFO>  \n" +
//                    "  </TX_INFO>    \n" +
//                    "</TX> \n"};

            if (tmpRetBkStr[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRetBkStr[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/TX/RETURN_CODE");
                    String tmpRetInfo = tmpBkReader.getFirstText("/TX/RETURN_MSG");
                    if (tmpRetCode != null && tmpRetCode.equals("000000")) {
                        for (int i = 0; i < actNumList.size(); i++) {
                            RetAtsBalCheckConfirmDetail tmpRetDetail = new RetAtsBalCheckConfirmDetail();
                            if (actNumList.get(i).dzFlag.equals("3")) {
                                tmpRetDetail.respCode = "-1";
                                tmpRetDetail.respInfo = "建行外联平台渠道暂不支持对账不平情况";
                                tmpRetDetail.actNum = actNumList.get(i).actNum;
                            } else {
                                tmpRetDetail.respCode = "0";
                                tmpRetDetail.respInfo = tmpRetInfo;
                                tmpRetDetail.actNum = actNumList.get(i).actNum;
                            }
                            retAts.writeRetDetail(tmpRetDetail, p_req7509.isBatch);
                        }
                    } else {
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo(tmpRetInfo);
                    }
                } else {
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpSbMsg.toString());
                }
            } else {
                retAts.writeRespCode("-1");
                retAts.writeRespInfo(tmpRetBkStr[1]);
            }
        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts.writeEnd();
        return retAts;
    }
}
